package com.mall.sun.controller;

import com.mall.sun.exception.Errors;
import com.mall.sun.model.dto.GoodDTO;
import com.mall.sun.model.dto.GoodSupplierDTO;
import com.mall.sun.model.vo.ResultVO;
import com.mall.sun.service.GoodsService;
import com.mall.sun.service.SupplierService;
import com.mall.sun.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * description:  <br>
 * date: 2021/12/11 14:29 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;
    @Autowired
    GoodsService goodsService;
    @PutMapping("/update")
    public ResultVO updateSupplier(@RequestBody GoodSupplierDTO goodSupplierDTO){
        System.out.println(goodSupplierDTO);
        try {
            return CommonUtil.ajaxReturn(supplierService.updateSupplier(goodSupplierDTO));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return CommonUtil.errorReturn(Errors.Update_Supplier_Error);
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete")
    public ResultVO deleteSupplier(@RequestBody GoodSupplierDTO goodSupplierDTO){
        return CommonUtil.ajaxReturn(goodsService.removeById(goodSupplierDTO.getGId()));
    }
    @GetMapping("/count")
    public ResultVO countSupplier(){
        return CommonUtil.ajaxReturn(supplierService.countSupplier());
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete/multi")
    public ResultVO deleteMulti(@RequestBody GoodSupplierDTO[] supplierDTOS){
        try {
            for (GoodSupplierDTO supplierDTO : supplierDTOS) {
                supplierService.removeById(supplierDTO.getGId());
            }
            return CommonUtil.ajaxReturn("删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return CommonUtil.ajaxReturn("删除失败");
    }
}
